#include <iostream>
#include <unistd.h>
#include <pthread.h>
#include <string>
using namespace std;
class task
{
private:
    int _a;
    int _b;
public:
    task(int a,int b):
        _a(a),
        _b(b)
    {}
    int execute()
    {
        return _a+_b;
    }
    ~task(){}
};
class Res
{
private:
    int _res;
public:
    Res(int res):_res(res){}
    int getresult()
    {
        return _res;
    }
};
void* routine(void* args)
{
    task* t=(task*)args;
    Res* r=new Res(t->execute());
    sleep(1);
    //return r;
    pthread_exit(r);
}
//线程终止问题：
//1，线程的入口函数return就是线程终止了
//2.线程不能用exit()来终止，因为exit是用来终止进程的！！！任意一个线程在代码中调用exit都会使整个进程退出
//3.pthread_exit()
int main()
{
    pthread_t tid;
    task* t =new task(4,2);
    pthread_create(&tid,nullptr,routine,t);
    //pthread_detach(tid);
    Res* r=nullptr;
    pthread_join(tid,(void**)&r);
    int n=r->getresult();
    cout<<"新进程结束,运行结果："<<n<<endl;
    delete t;
    delete r;
    return 0;
}